{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KLHiTPXNTf2a"
      },
      "source": [
        "##### Copyright 2025 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "oTuT5CsaTigz"
      },
      "outputs": [],
      "source": [
        "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-LiUeU4em33f"
      },
      "source": [
        "# Gemini API: Translate a public domain book"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "D7KnozoY6B3I"
      },
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Translate_a_Public_Domain_Book.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SRKGwj7Cu2CU"
      },
      "source": [
        "In this notebook, you will explore Gemini model as a translation tool, demonstrating how to prepare data, create effective prompts, and save results into a `.txt` file.\n",
        "\n",
        "This approach significantly improves knowledge accessibility. Various sources provide open-source books. In this notebook, you will use [Project Gutenberg](https://www.gutenberg.org/) as a resource.\n",
        "\n",
        "\n",
        "This platform provides access to a wide range of books available for free download in PDF, eBook, TXT formats and more."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "Gq_NixYDpsLN"
      },
      "outputs": [],
      "source": [
        "%pip install -U -q \"google-genai>=1.0.0\" tqdm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "9fxS1iFpEVTH"
      },
      "outputs": [],
      "source": [
        "from tqdm import tqdm\n",
        "\n",
        "from IPython.display import Markdown"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FQOGMejVu-6D"
      },
      "source": [
        "## Configure your API key\n",
        "\n",
        "To run the following cell, your API key must be stored in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "ysayz8skEfBW"
      },
      "outputs": [],
      "source": [
        "from google import genai\n",
        "from google.colab import userdata\n",
        "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
        "\n",
        "client = genai.Client(api_key=GOOGLE_API_KEY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "m1WJgnNmv8uM"
      },
      "source": [
        "## Data preparation"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Quunix4gwB-7"
      },
      "source": [
        "You will translate a well-known book written by Arthur Conan Doyle from Polish (trans. Eugenia Żmijewska) to English about the detective Sherlock Holmes. Here are the titles in Polish and English:\n",
        "\n",
        "\n",
        "\n",
        "* Polish Title: *Tajemnica Baskerville'ów: dziwne przygody Sherlocka Holmes*\n",
        "\n",
        "* English Title: *The Hound of the Baskervilles*"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "SPR8qlylbrdF"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
            "                                 Dload  Upload   Total   Spent    Left  Speed\n",
            "100  277k  100  277k    0     0   324k      0 --:--:-- --:--:-- --:--:--  324k\n"
          ]
        }
      ],
      "source": [
        "!curl https://www.gutenberg.org/cache/epub/34079/pg34079.txt > Sherlock.txt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "pPbU4n4CAEKu"
      },
      "outputs": [],
      "source": [
        "with open(\"/content/Sherlock.txt\") as f:\n",
        "      book = f.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-hPhMBNLNiH-"
      },
      "source": [
        "Books contain all sorts of fictional or historical descriptions, some of them rather literal and might cause the model to stop from performing translation query. To prevent some of those exceptions users are able to change `safety_setting` from default to more open approach."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "SsTS7UBCpwCo"
      },
      "outputs": [],
      "source": [
        "from google.genai import types\n",
        "safety_settings =[\n",
        "            types.SafetySetting(\n",
        "              category=types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,\n",
        "              threshold=types.HarmBlockThreshold.BLOCK_NONE,\n",
        "            ),\n",
        "            types.SafetySetting(\n",
        "              category=types.HarmCategory.HARM_CATEGORY_HARASSMENT,\n",
        "              threshold=types.HarmBlockThreshold.BLOCK_NONE,\n",
        "            ),\n",
        "            types.SafetySetting(\n",
        "              category=types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,\n",
        "              threshold=types.HarmBlockThreshold.BLOCK_NONE,\n",
        "            ),\n",
        "            types.SafetySetting(\n",
        "              category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,\n",
        "              threshold=types.HarmBlockThreshold.BLOCK_NONE,\n",
        "            )\n",
        "        ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i_xbb7Ce_NI9"
      },
      "source": [
        "Go ahead and initialize the Gemini Flash model for this task."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "iMFiWDxUPd5u"
      },
      "outputs": [],
      "source": [
        "MODEL_ID=\"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\",\"gemini-3-pro-preview\"] {\"allow-input\":true, isTemplate: true}\n",
        "\n",
        "\n",
        "def generate_output(prompt):\n",
        "    response = client.models.generate_content(\n",
        "        model=MODEL_ID,\n",
        "        contents=prompt,\n",
        "        config=types.GenerateContentConfig(\n",
        "          safety_settings=safety_settings\n",
        "        )\n",
        "\n",
        "    )\n",
        "\n",
        "    try:\n",
        "        return response.text\n",
        "    except Exception as ex:\n",
        "        raise ex"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HKdF4PQxGRlZ"
      },
      "source": [
        "Begin by counting how many tokens are in the book to know if you need to proceed with the next step (split text into smaller chunks)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XSxTHrcNJ3Te"
      },
      "source": [
        "## Token Information\n",
        "\n",
        "LLMs are constrained by token limits. In order to stay within the limit of Gemini Flash, it's important to ensure the following:\n",
        "\n",
        "* Input token limit should not exceed: 1,048,576 tokens\n",
        "* Output token limit should not exceed: 8,192 tokens\n",
        "\n",
        "Since input chunks of post-translation text will serve as outputs, it's advisable to split them into smaller segments.\n",
        "\n",
        "As you will see in the example, there are over 1000 small chunks. Calling API for the token count of each one would take a long time. Because of this estimate 1 token is equal to 4 characters will be used.\n",
        "\n",
        "To account for the possible miscalculation of tokens and output potentially being longer than input each chunk will use up to 4*5000 characters. Remember to adjust the maximum to your needs.\n",
        "\n",
        "For more details on tokens in Gemini models, refer to the documentation [here](https://ai.google.dev/gemini-api/docs/models/gemini\n",
        ")."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SFdNjziVXKI2"
      },
      "source": [
        "### Split text into chunks\n",
        "Adjust the number of whitespace characters used for splitting based on the book you are working with.\n",
        "\n",
        "Then, ensure to consider token limitations to receive complete responses from the model.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "-0Su5klXZH-w"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Number of chunks: 1802\n"
          ]
        }
      ],
      "source": [
        "chunks = book[:book.find(\"END OF THE PROJECT GUTENBERG EBOOK TAJEMNICA BASKERVILLE'ÓW: DZIWNE PRZYGODY SHERLOCKA HOLMES\")].split(\"\\n\\n\")\n",
        "\n",
        "num_chunks = len(chunks)\n",
        "print(f\"Number of chunks: {num_chunks}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "iptclxMgLxjC"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[134.5, 16.25, 6.5, 7.25, 25.0, 4.0, 58.25, 26.0, 50.25, 148.0, 10.25, 11.0, 7.5, 8.5, 10.0, 9.5, 9.25, 7.75, 9.25, 9.5, 7.25, 11.25, 0.75, 5.0, 136.75, 22.25, 37.0, 12.5, 73.25, 45.0, 11.5, 24.25, 8.5, 45.5, 11.75, 31.75, 50.0, 48.0, 45.25, 22.0, 38.25, 52.25, 6.75, 6.25, 3.75, 54.25, 19.5, 15.75, 24.25, 45.25, 4.5, 152.75, 22.0, 72.0, 135.0, 91.5, 9.75, 53.5, 38.0, 5.25, 120.0, 128.0, 37.5, 9.25, 3.5, 10.25, 15.75, 11.25, 15.5, 27.75, 31.25, 16.5, 13.75, 10.25, 73.25, 44.5, 66.0, 13.5, 28.0, 11.25, 43.0, 33.5, 1.0, 7.5, 14.5, 17.5, 6.25, 5.5, 4.0, 78.5, 16.5, 9.0, 111.0, 17.75, 35.75, 34.5, 15.5, 21.0, 30.5, 57.5, 33.75, 21.75, 158.75, 60.5, 39.0, 110.0, 125.75, 124.5, 66.0, 55.75, 40.25, 63.0, 54.5, 84.75, 45.0, 33.5, 90.5, 138.25, 37.75, 10.0, 38.75, 5.5, 10.25, 12.25, 10.0, 66.0, 19.5, 125.75, 62.5, 50.25, 59.25, 44.5, 131.25, 65.75, 20.25, 9.0, 65.5, 32.0, 50.75, 52.75, 68.5, 60.25, 48.25, 54.25, 10.0, 15.5, 8.5, 92.75, 1.75, 17.0, 24.25, 150.75, 29.75, 106.75, 103.75, 76.25, 59.75, 44.75, 76.25, 126.25, 82.75, 5.0, 1.75, 5.75, 21.25, 14.25, 1.25, 2.0, 26.0, 9.75, 14.75, 6.25, 2.25, 12.0, 31.5, 7.0, 18.75, 8.0, 1.75, 5.5, 7.25, 6.0, 1.75, 5.25, 28.0, 10.75, 14.75, 12.0, 8.0, 5.75, 2.25, 20.0, 14.75, 9.75, 12.75, 39.5, 9.75, 12.5, 5.25, 20.0, 9.0, 4.75, 5.5, 9.0, 1.75, 10.75, 10.0, 9.75, 5.75, 10.0, 22.25, 4.75, 12.25, 23.5, 15.0, 9.5, 83.25, 42.25, 6.5, 23.75, 18.25, 7.75, 9.25, 22.75, 3.5, 95.75, 16.75, 77.75, 88.75, 5.25, 10.25, 42.25, 7.0, 62.0, 10.25, 149.25, 15.25, 154.5, 9.0, 36.0, 10.5, 45.5, 70.75, 32.5, 2.75, 18.5, 10.75, 49.25, 6.5, 20.75, 30.75, 5.25, 9.25, 3.0, 7.25, 14.75, 8.0, 8.25, 78.5, 77.75, 81.5, 7.25, 8.0, 14.5, 8.25, 21.5, 12.25, 7.25, 2.25, 71.0, 9.0, 1.75, 8.25, 14.5, 3.75, 118.25, 6.0, 11.0, 15.75, 136.5, 11.5, 18.0, 20.25, 47.0, 6.0, 22.5, 3.5, 94.75, 9.0, 51.0, 10.0, 88.0, 0.75, 5.75, 51.0, 75.25, 15.25, 37.0, 24.75, 13.0, 62.0, 28.75, 20.5, 11.0, 35.5, 10.75, 31.25, 12.5, 20.75, 33.75, 25.5, 26.75, 33.0, 62.5, 3.5, 19.25, 7.75, 21.75, 64.25, 34.5, 22.25, 27.5, 23.0, 27.0, 40.25, 11.25, 40.0, 66.25, 20.25, 13.5, 61.75, 16.5, 23.5, 15.5, 6.5, 19.0, 198.25, 16.75, 34.75, 4.75, 140.75, 24.25, 24.5, 3.25, 10.5, 15.5, 10.25, 12.5, 9.75, 33.25, 19.5, 45.5, 15.75, 18.25, 17.25, 85.75, 20.0, 32.5, 36.25, 25.5, 22.75, 127.75, 26.75, 30.25, 17.0, 44.25, 5.25, 12.5, 26.75, 7.0, 39.5, 36.5, 70.25, 11.0, 5.5, 13.5, 16.75, 50.75, 12.0, 32.5, 124.25, 11.0, 53.25, 43.0, 21.5, 6.75, 4.75, 4.5, 88.75, 4.75, 113.25, 25.75, 119.25, 25.25, 44.5, 7.0, 19.25, 17.0, 28.5, 14.5, 29.5, 6.75, 22.75, 32.0, 31.5, 3.5, 8.75, 4.25, 19.5, 4.25, 39.75, 4.75, 38.75, 2.5, 151.25, 0.75, 4.25, 64.0, 15.25, 19.0, 15.25, 3.0, 47.25, 27.25, 23.5, 9.25, 16.5, 37.5, 26.25, 17.5, 11.0, 48.25, 28.5, 31.0, 6.0, 9.0, 9.25, 52.25, 11.75, 22.0, 27.25, 10.75, 28.0, 8.0, 16.5, 83.75, 35.25, 10.5, 2.25, 5.0, 101.25, 7.0, 5.75, 37.5, 22.25, 9.0, 4.5, 12.75, 4.25, 48.75, 2.75, 66.0, 25.5, 43.75, 31.75, 3.25, 19.25, 11.0, 10.75, 20.0, 7.5, 42.25, 6.75, 28.25, 3.75, 7.5, 8.75, 14.25, 38.75, 64.0, 56.5, 21.5, 54.25, 21.5, 16.75, 12.25, 89.0, 44.75, 8.0, 61.75, 12.5, 52.5, 11.0, 8.75, 27.0, 29.5, 34.25, 29.75, 9.5, 45.0, 10.0, 22.0, 25.5, 7.5, 22.0, 26.5, 10.25, 5.75, 15.75, 25.5, 88.25, 57.75, 18.75, 11.25, 12.25, 9.75, 21.0, 12.0, 31.25, 14.25, 25.75, 16.25, 71.75, 43.75, 20.0, 15.0, 24.5, 8.25, 42.5, 10.75, 37.0, 48.25, 5.0, 7.0, 6.0, 7.75, 1.75, 10.0, 14.75, 4.75, 28.75, 25.25, 1.75, 20.0, 97.5, 5.75, 128.75, 8.0, 1.75, 10.5, 7.75, 48.5, 4.75, 3.0, 7.75, 1.75, 22.5, 7.0, 8.0, 132.0, 1.0, 4.25, 47.0, 48.0, 12.25, 116.5, 17.5, 134.5, 8.0, 7.5, 11.5, 17.5, 20.0, 66.0, 12.75, 31.25, 44.75, 5.75, 41.25, 10.0, 35.75, 52.75, 26.25, 40.5, 90.5, 78.5, 9.25, 17.0, 36.75, 19.5, 35.0, 66.0, 48.75, 72.5, 19.25, 16.75, 14.5, 9.25, 44.5, 24.0, 27.75, 4.75, 19.25, 71.25, 39.0, 70.0, 8.75, 18.75, 41.75, 32.0, 11.25, 19.0, 50.25, 20.75, 30.5, 18.5, 36.25, 24.5, 9.75, 49.5, 20.0, 64.5, 50.25, 34.75, 26.75, 11.75, 3.25, 38.25, 7.25, 39.75, 46.75, 17.5, 49.25, 6.75, 35.5, 73.0, 112.25, 39.5, 28.25, 70.5, 67.5, 16.75, 34.5, 40.0, 1.25, 6.75, 61.25, 60.75, 22.5, 32.0, 18.0, 12.0, 32.0, 35.0, 7.0, 46.5, 31.75, 105.75, 43.0, 35.5, 32.75, 26.5, 4.5, 29.5, 3.5, 8.0, 34.75, 8.0, 13.0, 15.5, 43.25, 53.75, 15.0, 37.0, 41.75, 13.25, 34.75, 23.25, 25.5, 58.0, 72.0, 31.75, 35.5, 4.0, 67.75, 6.75, 20.0, 3.25, 28.25, 25.5, 29.0, 4.25, 55.25, 9.0, 10.25, 22.25, 10.25, 9.75, 16.5, 37.0, 98.25, 11.5, 10.0, 21.5, 55.75, 23.5, 44.25, 36.5, 29.25, 64.25, 67.75, 5.0, 77.75, 10.75, 32.75, 11.75, 27.75, 7.0, 6.0, 112.5, 40.0, 30.5, 13.0, 16.5, 10.75, 38.0, 4.25, 8.0, 40.5, 8.75, 23.5, 13.75, 12.5, 7.5, 36.5, 12.0, 30.75, 16.5, 10.75, 16.5, 1.75, 41.0, 15.75, 24.5, 13.25, 7.5, 53.75, 4.5, 6.0, 6.0, 37.75, 29.75, 44.25, 219.5, 18.75, 18.5, 8.75, 39.75, 8.75, 91.0, 11.5, 12.0, 10.5, 20.25, 14.5, 20.0, 18.75, 6.0, 13.5, 20.75, 16.25, 9.75, 26.0, 34.5, 51.5, 67.5, 43.5, 12.75, 188.0, 26.75, 9.0, 75.0, 9.25, 58.5, 91.75, 40.0, 39.25, 53.75, 35.25, 115.0, 15.75, 112.25, 11.0, 8.0, 6.75, 45.25, 45.75, 16.25, 35.0, 80.0, 17.5, 1.5, 9.25, 41.5, 16.25, 34.75, 53.5, 65.25, 65.5, 57.25, 47.0, 83.0, 128.5, 148.0, 120.25, 92.5, 211.5, 40.25, 52.0, 39.5, 81.0, 127.5, 160.5, 17.75, 43.0, 111.25, 53.0, 29.0, 65.25, 14.0, 8.5, 23.75, 8.0, 9.5, 13.75, 26.75, 33.5, 111.25, 28.75, 10.0, 30.0, 88.5, 69.5, 49.0, 56.25, 21.25, 47.25, 60.0, 28.75, 1.0, 9.5, 4.5, 17.5, 4.0, 20.75, 86.25, 97.0, 41.5, 73.25, 21.75, 44.75, 32.25, 9.0, 38.25, 30.75, 23.0, 19.5, 48.25, 19.25, 34.25, 26.5, 16.75, 17.5, 19.25, 5.75, 30.75, 9.75, 61.0, 26.0, 22.75, 11.5, 12.75, 48.0, 35.75, 23.0, 8.75, 25.75, 65.25, 63.75, 21.5, 38.0, 53.0, 27.25, 19.75, 34.0, 21.75, 35.0, 44.25, 7.75, 13.25, 4.0, 10.25, 1.75, 80.0, 7.75, 50.0, 5.5, 241.0, 31.0, 30.5, 36.75, 34.0, 22.75, 54.5, 36.25, 30.5, 38.0, 48.0, 8.75, 216.5, 26.25, 34.75, 37.0, 27.25, 27.0, 39.25, 33.0, 36.75, 26.0, 16.75, 78.25, 39.75, 11.25, 5.0, 13.5, 27.25, 54.0, 12.75, 16.25, 6.75, 39.5, 19.5, 19.25, 51.0, 7.25, 25.25, 57.75, 12.75, 16.5, 11.25, 8.75, 37.0, 6.25, 25.0, 19.5, 36.5, 15.0, 51.5, 6.75, 10.75, 45.25, 16.0, 41.5, 272.75, 7.0, 13.5, 7.25, 28.0, 33.75, 19.75, 15.5, 10.0, 21.0, 27.0, 107.25, 16.0, 8.0, 47.25, 19.75, 35.0, 5.5, 8.5, 10.5, 26.5, 39.25, 69.5, 15.25, 24.0, 26.5, 15.25, 9.5, 19.5, 12.5, 7.75, 3.5, 15.0, 3.5, 22.75, 10.0, 32.75, 26.25, 40.75, 6.25, 87.75, 5.75, 17.75, 19.25, 4.5, 29.5, 49.5, 71.0, 8.75, 17.5, 71.5, 21.75, 52.75, 80.0, 32.5, 36.5, 62.0, 51.5, 67.0, 52.75, 42.25, 47.0, 59.5, 24.75, 21.75, 33.75, 59.75, 28.0, 41.0, 0.75, 9.0, 62.75, 16.75, 37.25, 45.75, 73.25, 70.0, 52.25, 20.25, 44.0, 25.0, 107.0, 68.0, 46.5, 51.5, 66.25, 37.75, 22.5, 64.75, 54.0, 14.25, 65.0, 60.0, 15.25, 5.25, 25.75, 18.75, 39.5, 14.5, 37.25, 41.0, 26.0, 57.0, 12.5, 13.5, 9.0, 3.0, 17.0, 5.25, 3.5, 6.0, 14.5, 7.0, 68.0, 3.0, 91.5, 9.0, 10.5, 17.75, 28.25, 9.75, 30.25, 18.75, 63.5, 13.0, 26.75, 10.0, 17.75, 13.25, 14.5, 64.75, 36.0, 21.75, 73.25, 16.75, 126.0, 55.75, 119.25, 13.5, 6.5, 17.75, 4.25, 69.25, 7.0, 7.0, 8.75, 48.75, 6.5, 92.25, 34.5, 126.0, 34.75, 40.25, 20.25, 27.5, 7.0, 18.0, 6.75, 14.25, 10.25, 13.5, 5.0, 4.75, 1.75, 5.0, 39.5, 29.5, 22.75, 29.25, 6.0, 52.25, 34.0, 49.5, 10.25, 25.5, 6.5, 25.25, 11.25, 79.0, 1.0, 9.25, 69.5, 81.0, 24.25, 60.0, 57.75, 79.0, 98.5, 14.75, 54.75, 19.75, 8.25, 20.0, 5.75, 32.0, 8.25, 9.5, 10.75, 30.0, 15.75, 12.75, 17.0, 23.5, 10.75, 1.75, 8.0, 29.0, 48.0, 7.5, 53.0, 25.0, 26.5, 6.5, 12.75, 9.5, 12.75, 15.25, 45.75, 52.5, 12.25, 16.25, 38.25, 62.75, 8.5, 42.0, 17.75, 23.25, 12.25, 6.5, 9.75, 29.75, 3.5, 13.25, 44.0, 4.0, 19.0, 85.0, 27.0, 19.0, 15.0, 9.25, 10.5, 50.5, 7.25, 18.0, 38.75, 3.25, 120.25, 13.25, 9.75, 13.25, 23.75, 43.5, 47.75, 56.0, 34.0, 35.5, 158.0, 33.5, 17.25, 42.5, 71.5, 7.0, 236.0, 50.0, 14.75, 24.5, 4.5, 12.25, 64.75, 8.0, 18.75, 16.75, 34.25, 68.5, 25.25, 72.25, 30.0, 87.25, 28.0, 57.75, 21.5, 32.75, 23.5, 15.5, 62.75, 11.5, 27.75, 28.75, 7.75, 46.5, 57.75, 24.5, 75.25, 218.25, 10.5, 61.25, 123.5, 81.0, 37.75, 31.75, 1.25, 4.25, 61.75, 6.5, 13.25, 71.0, 15.75, 9.5, 5.25, 51.0, 8.25, 92.25, 3.0, 47.5, 18.0, 41.5, 6.75, 21.75, 17.25, 35.0, 6.5, 28.5, 1.75, 10.25, 3.5, 30.25, 58.0, 7.5, 31.0, 157.25, 9.5, 99.5, 9.75, 6.75, 48.25, 36.25, 71.25, 36.75, 43.75, 4.5, 49.5, 2.0, 37.5, 19.5, 62.0, 6.5, 20.25, 42.0, 23.25, 10.0, 11.5, 19.5, 174.25, 20.0, 41.0, 10.25, 55.0, 11.25, 31.5, 4.75, 100.75, 14.0, 7.75, 16.25, 7.5, 15.75, 11.5, 14.75, 6.5, 22.25, 17.75, 37.25, 16.5, 40.0, 18.0, 11.5, 4.75, 9.25, 67.25, 31.5, 39.5, 16.5, 74.5, 52.0, 128.5, 70.25, 35.25, 9.5, 54.5, 6.75, 22.5, 19.0, 30.0, 28.75, 12.75, 6.5, 12.25, 37.25, 103.5, 78.25, 8.25, 57.75, 55.5, 22.5, 18.0, 58.25, 37.75, 51.75, 24.75, 8.75, 12.25, 37.0, 13.25, 29.0, 24.0, 12.5, 61.5, 8.0, 3.0, 7.25, 38.75, 9.0, 12.5, 86.25, 38.0, 10.75, 26.75, 41.25, 6.0, 4.75, 27.25, 53.75, 15.5, 42.0, 35.25, 64.75, 6.5, 15.5, 17.75, 45.25, 9.25, 85.5, 5.75, 33.0, 12.75, 113.5, 15.75, 64.75, 5.0, 22.5, 24.25, 7.5, 43.0, 4.5, 36.75, 1.5, 4.5, 94.25, 88.5, 15.25, 72.5, 34.0, 11.0, 35.5, 8.75, 74.0, 46.5, 39.5, 55.0, 21.25, 14.25, 12.25, 3.0, 35.5, 30.75, 11.25, 35.0, 65.75, 55.25, 60.5, 4.5, 7.25, 23.5, 11.5, 44.5, 14.75, 43.75, 12.0, 39.25, 22.25, 43.5, 19.5, 7.25, 61.75, 17.0, 14.0, 6.5, 23.25, 7.75, 11.25, 48.25, 26.5, 49.5, 8.25, 60.75, 11.5, 22.25, 27.75, 5.75, 68.25, 3.25, 10.0, 34.75, 11.25, 8.75, 12.5, 29.25, 11.0, 3.5, 14.5, 36.0, 65.0, 7.25, 24.0, 10.75, 53.75, 16.5, 23.0, 7.25, 32.5, 5.75, 1.75, 36.0, 39.25, 4.25, 29.75, 5.5, 21.0, 75.5, 40.5, 9.5, 54.5, 4.0, 11.5, 19.0, 21.25, 53.75, 47.0, 23.25, 6.5, 34.75, 50.25, 5.0, 52.25, 9.5, 66.75, 9.25, 7.5, 26.75, 24.25, 16.0, 20.0, 28.75, 137.5, 19.0, 137.5, 61.25, 5.0, 23.25, 3.5, 17.5, 47.5, 17.0, 1.75, 21.75, 33.5, 9.75, 8.0, 20.0, 43.75, 10.0, 132.0, 49.25, 8.25, 89.0, 1.25, 4.5, 110.25, 65.75, 52.5, 9.5, 35.25, 16.0, 5.25, 2.5, 32.0, 29.75, 14.25, 50.5, 4.0, 7.75, 6.5, 34.5, 23.25, 17.25, 58.25, 126.5, 14.25, 20.5, 7.75, 16.25, 37.25, 62.75, 115.0, 28.0, 8.75, 12.0, 45.75, 26.0, 9.75, 96.5, 69.75, 15.5, 58.25, 30.5, 34.0, 92.75, 34.5, 52.75, 10.5, 20.0, 39.75, 17.25, 5.5, 71.0, 6.25, 19.0, 28.75, 23.75, 23.0, 32.5, 15.0, 26.75, 26.75, 58.0, 59.0, 12.0, 20.0, 33.5, 21.0, 110.75, 33.5, 36.25, 3.75, 12.5, 6.0, 14.0, 1.75, 2.5, 3.0, 5.5, 17.5, 11.75, 48.5, 4.25, 35.5, 45.0, 7.75, 20.75, 8.75, 51.5, 45.0, 61.25, 42.75, 10.0, 36.25, 37.25, 47.5, 73.75, 59.5, 20.5, 12.25, 59.25, 90.25, 24.25, 57.5, 249.5, 1.0, 4.0, 62.5, 29.25, 70.75, 12.5, 235.5, 31.0, 111.75, 40.75, 73.0, 40.0, 133.25, 57.25, 66.75, 129.25, 93.0, 43.75, 54.75, 53.25, 39.5, 162.25, 20.75, 56.5, 64.0, 65.5, 39.5, 37.5, 27.25, 66.75, 65.25, 63.0, 75.0, 27.5, 105.25, 99.5, 73.0, 67.0, 25.25, 63.5, 88.5, 61.0, 47.5, 40.5, 36.75, 169.5, 89.5, 88.5, 80.75, 48.25, 30.75, 94.0, 30.25, 76.0, 120.75, 66.75, 9.25, 2.25, 7.5, 3.25, 67.5, 51.25, 8.25, 45.25, 120.25, 17.0, 10.5, 74.0, 15.5, 18.75, 25.25, 6.5, 9.5, 12.0, 28.0, 7.75, 16.0, 21.5, 26.25, 26.0, 21.25, 25.0, 23.25, 26.75, 20.25, 24.25, 24.75, 22.5, 23.5, 24.0, 25.0, 19.0, 21.75, 22.25, 16.5, 24.25, 23.0, 23.0, 19.0, 23.5, 31.25, 22.25, 17.5, 18.0, 24.75, 19.0, 25.5, 20.0, 25.25, 23.5, 17.25, 24.75, 21.25, 27.75, 17.0, 22.25, 21.25, 16.0, 24.5, 21.25, 18.5, 20.25, 19.5, 18.5, 1.0]\n"
          ]
        }
      ],
      "source": [
        "estimated_token_counts = []\n",
        "\n",
        "for chunk in chunks:\n",
        "    if not chunk:  # Check if chunk is empty or None\n",
        "        continue  # Skip this iteration if chunk is empty\n",
        "\n",
        "    # Process non-empty chunk\n",
        "    estimated_token_count = len(chunk)/4\n",
        "    estimated_token_counts.append(estimated_token_count)\n",
        "\n",
        "# You can print number of estimated tokens in each non empty chunk to see what are you working with\n",
        "print(estimated_token_counts)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QfEScOTMgZbx"
      },
      "source": [
        "Identify the largest number of tokens in a chunk to check if any exceed your token limitations. In this case, the chunks are very small, so this should not be an issue.\n",
        "\n",
        "If you encounter this problem, consider splitting the text into even smaller chunks."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "q0EtN8D6ynXi"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "272.75"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "max(estimated_token_counts)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ozz3zNX-3VVL"
      },
      "source": [
        "### Group smaller chunks together\n",
        "As observed above, certain chunks contain very few tokens. While the LLM can translate these sentences accurately, providing larger context can enhance its understanding of the content.\n",
        "\n",
        "Let's combine smaller chunks into larger ones for improved results."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "SlWJtNgS6AdW"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "13\n"
          ]
        }
      ],
      "source": [
        "# Combine short chunks into larger groups based on token counts.\n",
        "def chunk_grouping(chunks, token_counts, max_len=5000):\n",
        "    grouped_chunks = []\n",
        "    current_group = \"\"\n",
        "    current_token_sum = 0\n",
        "\n",
        "    # Process each chunk and group them based on token limits\n",
        "    for chunk, count in zip(chunks, token_counts):\n",
        "        # Skip chunks that exceed the max token limit\n",
        "        if count > max_len:\n",
        "            continue\n",
        "\n",
        "        # Add a new chunk if there is space available in the current group.\n",
        "        if current_token_sum + 1 + count <= max_len:\n",
        "            current_group += \"\\n\\n\" + chunk\n",
        "            current_token_sum += 1 + count  # Count in 1 token for newlines\n",
        "\n",
        "        # If adding this chunk exceeds the current group's capacity, start a new group\n",
        "        else:\n",
        "            grouped_chunks.append(current_group)\n",
        "            current_group = chunk\n",
        "            current_token_sum = count\n",
        "\n",
        "    if current_group:  # Add the last remaining group\n",
        "        grouped_chunks.append(current_group)\n",
        "\n",
        "    return grouped_chunks\n",
        "\n",
        "\n",
        "chunks = chunk_grouping(chunks, estimated_token_counts)\n",
        "print(len(chunks))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2RcTf9Q9cubd"
      },
      "source": [
        "The last step of splitting is checking if the non-estimated token counts are acceptable."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "j1rqKAplctEI"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[6309, 6465, 6603, 6371, 6761, 6534, 6271, 7111, 6392, 6718, 6821, 5670, 3242]"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "token_counts = []\n",
        "for chunk in chunks:\n",
        "  response = client.models.count_tokens(\n",
        "      model = MODEL_ID,\n",
        "      contents = chunk,\n",
        "  )\n",
        "  token_count = response.total_tokens\n",
        "  token_counts.append(token_count)\n",
        "token_counts"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RqNZ-JameFOx"
      },
      "source": [
        "Despite using estimates, this split minimizes the calls necessary to translate the book well, and most chunks are of similar token counts to each other."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B3GFZw6viwO7"
      },
      "source": [
        "## Translation\n",
        "\n",
        "Your book is ready for translations! The selected book has been divided into 13 larger chunks."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BkbGQUWsj28i"
      },
      "source": [
        "### Prompt\n",
        "\n",
        "Specify the original language of your book and the language you would like it to be translated into.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "ZkA6L5URdFl9"
      },
      "outputs": [],
      "source": [
        "def translations(text,\n",
        "                 inputLanguage='Polish',\n",
        "                 targetLanguage='English'):\n",
        "\n",
        "    translation_prompt = f\"\"\"\n",
        "      As a professional book translator,\n",
        "      translate the following book from {inputLanguage} into {targetLanguage}.\n",
        "\n",
        "      Book to Translate:\n",
        "      {text}\n",
        "\n",
        "    \"\"\"\n",
        "    return translation_prompt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1RhRUwcgkZRe"
      },
      "source": [
        "### Translate chunks\n",
        "\n",
        "The translation time depends on the amount of text being processed. Our selected book was 318 KB and the entire translation took 20 minutes.\n",
        "\n",
        "Please note that larger texts may take several hours to complete."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "2jiU5EJ02-KT"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "100%|██████████| 13/13 [04:54<00:00, 22.64s/it]\n"
          ]
        }
      ],
      "source": [
        "results = []\n",
        "index = 0\n",
        "translation = None\n",
        "\n",
        "for chunk in tqdm(chunks):\n",
        "    try:\n",
        "        translation = generate_output(translations(chunk))\n",
        "        results.append(translation)\n",
        "    except Exception as ex:\n",
        "        print(f\"Chunk was not included in translation due to possible sensitive content. Text: {chunk}\")\n",
        "\n",
        "    index += 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YL96QOTLP6nh"
      },
      "source": [
        "All done, now you can print the chunk before and after translation to see the results.\n",
        "\n",
        "You can see, that translation effectively maintains the narrative's novel quality, proper grammar and sentence structure.\n",
        "\n",
        "Next step will be joining chunks together and saving the translated book."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "FBM8YeghZUaQ"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "Doktor Mortimer skończył czytać tę niezwykłą opowieść, nasunął na\n",
              "oczy okulary i spojrzał na pana Sherlocka Holmes. Ten ziewnął\n",
              "i rzucił papierosa w ogień.\n",
              "\n",
              "-- No i cóż? -- rzekł.\n",
              "\n",
              "-- Czy historya ta nie jest interesującą?\n",
              "\n",
              "-- Zapewne, dla zbieracza starych legend i baśni.\n",
              "\n",
              "Doktor Mortimer wyjął z kieszeni gazetę.\n",
              "\n",
              "-- A teraz, panie Holmes -- rzekł -- pokażę panu coś aktualniejszego.\n",
              "Oto dziennik „Devon County Chronicle” z dnia 14-go Maja roku\n",
              "bieżącego. Jest w nim opis faktów, odnoszących się do śmierci sir\n",
              "Karola Baskerville, która zdarzyła się na kilka dni przed ową datą.\n",
              "\n",
              "Mój przyjaciel nachylił się i począł słuchać uważniej. Doktor\n",
              "Mortimer czytał:\n",
              "\n",
              "„Niedawna i nagła śmierć sir Karola Baskerville, kandydata liberalnego\n",
              "z hrabstwa Devon, przejęła całą naszą okolicę zdumieniem i trwogą.\n",
              "Jakkolwiek sir Karol Baskerville mieszkał w Baskerville Hall od\n",
              "niedawna, lecz jego uprzejmość, hojność i szlachetność, zjednały mu\n",
              "szacunek tych wszystkich, którzy mieli z nim do czynienia.\n",
              "W dzisiejszej epoce parweniuszów miło jest widzieć potomka\n",
              "starożytnego a podupadłego rodu, odzyskującego fortunę swych\n",
              "przodków i przywracającego świetność staremu nazwisku.\n",
              "\n",
              "„Sir Karol, jak wiadomo, zyskał duży majątek na spekulacyach w Afryce\n",
              "południowej, a będąc z natury przezornym, nie kusił dalej szczęścia,\n",
              "które mogłoby się odwrócić od niego, jak to czyni z innymi,\n",
              "i zrealizowawszy swoją fortunę, powrócił do Anglii.\n",
              "\n",
              "„Przed dwoma laty zaledwie osiadł w Baskerville Hall; znane są jego\n",
              "szerokie plany odnowienia starej siedziby i wprowadzenia ulepszeń\n",
              "w gospodarstwie rolnem. Śmierć przeszkodziła ich urzeczywistnieniu.\n",
              "\n",
              "„Będąc bezdzietnym, pragnął, aby cała okolica korzystała z jego\n",
              "fortuny i niejeden z sąsiadów ma ważne powody do opłakiwania jego\n",
              "przedwczesnego zgonu. Donosiliśmy często na tych szpaltach o jego\n",
              "hojnych ofiarach na instytucye publiczne.\n",
              "\n",
              "„Śledztwo nie wyjaśniło dotychczas okoliczności, towarzyszących\n",
              "śmierci sir Karola. Krążą tu najdziwaczniejsze legendy, przypisujące\n",
              "ów nagły zgon działaniu sił nadprzyrodzonych.\n",
              "\n",
              "„Sir Karol był wdowcem, uważano go powszechnie za dziwaka. Pomimo\n",
              "dużej fortuny, miał przyzwyczajenia proste, skromne. Personel służbowy\n",
              "w Baskerville Hall składał się z kamerdynera, nazwiskiem Barrymore,\n",
              "jego żony, kucharki i gospodyni w jednej osobie. Oboje zeznają, że\n",
              "zdrowie sir Karola w ostatnich czasach było nietęgie, że rozwijała się\n",
              "w nim choroba serca, objawiająca się bladością, brakiem tchu\n",
              "i częstemi omdleniami. Doktor Jerzy Mortimer, przyjaciel i domowy lekarz\n",
              "nieboszczyka, złożył zeznanie w tym samym duchu.\n",
              "\n",
              "„Sir Karol Baskerville zwykł był co wieczór, przed pójściem na\n",
              "spoczynek, przechadzać się po słynnej alei wiązów przed zamkiem.\n",
              "Małżonkowie Barrymore opowiadają, że wieczorom 4-go maja, ich pan\n",
              "oznajmił im, że nazajutrz wyrusza do Londynu i kazał pakować kuferek.\n",
              "\n",
              "„Owego wieczoru wyszedł, jak zwykle, na przechadzkę, wśród której\n",
              "wypalał cygaro.\n",
              "\n",
              "„Z tej przechadzki już nie powrócił.\n",
              "\n",
              "„O dwunastej Barrymore, widząc drzwi frontowe jeszcze otwarte,\n",
              "zaniepokoił się, i wziąwszy latarnię, poszedł szukać swego pana.\n",
              "W ciągu dnia deszcz padał, więc łatwo było dojrzeć ślady stóp sir\n",
              "Karola wzdłuż alei. W połowie drogi jest furtka, prowadząca na łąkę.\n",
              "\n",
              "„Były ślady, że sir Karol stał przy niej przez czas pewien,\n",
              "następnie skręcił znowu w aleję. Znaleziono jego trupa na jej końcu.\n",
              "\n",
              "„Pomiędzy innemi dotychczas nie wyjaśniono jednego faktu, wynikającego\n",
              "z zeznań Barrymora, a mianowicie, że ślady kroków jego pana zmieniły\n",
              "się z chwilą, gdy stał przy furtce i że odtąd szedł na palcach.\n",
              "\n",
              "„Niejaki Murphy, cygan, handlujący końmi, znajdował się wówczas na\n",
              "łące, w pewnej odległości od nieboszczyka, ale sam przyznaje, że był\n",
              "pijany. Powiada, że słyszał krzyki, ale nie może określić, skąd\n",
              "wychodziły.\n",
              "\n",
              "„Na ciele sir Karola nie było śladów przemocy lub gwałtu, a chociaż\n",
              "lekarz zeznaje, że wyraz twarzy nieboszczyka był tak zmieniony, że on,\n",
              "doktor Mortimer, w pierwszej chwili poznać go nie mógł, rzeczoznawcy\n",
              "tłómaczą to anewryzmem serca, przy którym zachodzą podobne objawy.\n",
              "\n",
              "„Sekcya wykazała przedawnioną wadę serca, sędzia śledczy potwierdził\n",
              "badanie lekarskie. Pożądanem jest, aby spadkobierca sir Karola mógł\n",
              "objąć jaknajprędzej majątek i prowadzić dalej chlubną działalność,\n",
              "przerwaną tak nagle i tak tragicznie.\n",
              "\n",
              "„Gdyby prozaiczne orzeczenie sędziego śledczego i rzeczoznawców nie\n",
              "położyło końca romantycznym historyom, krążącym na temat owej\n",
              "śmierci, nie łatwo byłoby znaleźć właściciela Baskerville Hall.\n",
              "\n",
              "„Domniemanym spadkobiercą jest pan Henryk Baskerville, syn młodszego\n",
              "brata sir Karola. Ów młodzieniec przed kilku laty wyruszył do Ameryki.\n",
              "Zarządzono poszukiwania. Dowie się on zapewne niebawem o zmianie swego\n",
              "losu”.\n",
              "\n",
              "       *       *       *       *       *\n",
              "\n",
              "Doktor Mortimer złożył gazetę i wsunął ją do kieszeni, mówiąc:\n",
              "\n",
              "-- To są fakty, powszechnie znane.\n",
              "\n",
              "-- Dziękuję panu za zwrócenie mojej uwagi na sprawę istotnie \n",
              "ciekawą -- rzekł pan Holmes. -- Czytałem o niej, coprawda, w dziennikach, \n",
              "ale w owym czasie byłem zajęty kameami, które zniknęły z muzeum\n",
              "Watykańskiego. Chciałem się przysłużyć Papieżowi i straciłem z oczu\n",
              "to, co się jednocześnie działo w Anglii. Ten artykuł, powiadasz pan,\n",
              "zawiera fakty, znane powszechnie?\n",
              "\n",
              "-- Tak.\n",
              "\n",
              "-- Zechciej więc pan uwiadomić mnie teraz o tem, czego nikt nie wie.\n",
              "\n",
              "Sherlock Holmes oparł się o poręcz fotelu i znowu złożył ręce tak,\n",
              "aby palce stykały się końcami.\n",
              "\n",
              "-- Uczynię to -- rzekł doktor Mortimer, zdradzając coraz większe\n",
              "wzburzenie. -- Powiem panu to, czego nie mówiłem jeszcze nikomu.\n",
              "Zataiłem to przed sędzią śledczym z pobudek, które pan zapewne\n",
              "zrozumie. Jako człowiek nauki, nie chciałem zdradzać się publicznie,\n",
              "iż wierzę w przesądy. Dalej, rozumiałem, że gdyby utwierdziła się\n",
              "wiara w nadprzyrodzone siły, rządzące w Baskerville Hall, nikt nie\n",
              "zechciałby objąć starego zamku w posiadanie. Dla tych dwóch powodów\n",
              "nie zeznałem przed sądem wszystkiego, co wiem -- albowiem nie\n",
              "zdałoby się to na nic sędziemu śledczemu -- ale z panem będę\n",
              "zupełnie szczery.\n",
              "\n",
              "„Okolica jest mało zaludniona, rezydencye rzadkie, a siedziby\n",
              "włościańskie rozrzucone na znacznej odległości od siebie.\n",
              "\n",
              "„Widywałem często sir Karola Baskerville, który był spragniony\n",
              "towarzystwa. Oprócz p. Frankland w Lafterhall i naturalisty,\n",
              "p. Stapleton, w pobliżu niema ludzi inteligentnych. Sir Karol\n",
              "był skryty, małomówny. Zbliżyliśmy się z powodu jego choroby,\n",
              "przytem łączyły nas wspólne upodobania naukowe. Przywiózł wiele\n",
              "ciekawych wiadomości z Afryki Południowej. Spędziliśmy niejeden\n",
              "miły wieczór na rozprawach o anatomii porównawczej.\n",
              "\n",
              "„W ciągu ostatnich paru miesięcy spostrzegłem, że system nerwowy sir\n",
              "Karola był nadwerężony. Wziął tak dalece ową legendę do serca, że\n",
              "choć co wieczór odbywał spacery, nic go nie mogło skłonić do\n",
              "wejścia na łąkę po zachodzie słońca. Prześladowała go wciąż obawa\n",
              "upiorów, i pytał mnie nieraz, czym nie widział jakiego dziwacznego\n",
              "stworzenia i czym nie słyszał szczekania. Te pytania zadawał mi zawsze\n",
              "głosem drżącym.\n",
              "\n",
              "„Wraziła mi się w pamięć jedna z moich wizyt u niego przed trzema\n",
              "tygodniami. Stał w sieni. Gdym schodził z wózka, spostrzegłem, że\n",
              "patrzy po przez moje ramię ze strachem w oczach. Odwróciłem się\n",
              "i spostrzegłem jakiś kształt dziwny, podobny do dużego, czarnego\n",
              "cielęcia. Przeleciało to po za moim wózkiem.\n",
              "\n",
              "„Sir Karol był tak wzburzony, że poszedłem szukać tego osobliwego\n",
              "zwierzęcia. Ale nigdzie go nie było. Mój pacyent nie mógł się\n",
              "uspokoić. Zostałem z nim przez cały wieczór i wtedy, tłómacząc swe\n",
              "wzburzenie, opowiedział mi całą tę historyę.\n",
              "\n",
              "„Wspominam o tym drobnym epizodzie, gdyż nabiera on znaczenia wobec\n",
              "tragedyi, która potem nastąpiła; na razie nie przywiązywałem do tego\n",
              "wagi i dziwiłem się wzburzeniu sir Karola.\n",
              "\n",
              "„Miał jechać do Londynu z mojej porady. Wiedziałem, że jest chory\n",
              "na serce i że ciągły niepokój źle wpływa na jego zdrowie. Sądziłem,\n",
              "że parę miesięcy, spędzonych na rozrywkach, uwolni go od tych mar\n",
              "i przywidzeń. Nasz wspólny przyjaciel, pan Stapleton, był tego samego\n",
              "zdania. I oto wynikło nieszczęście.\n",
              "\n",
              "„Zaraz po śmierci sir Karola, kamerdyner Barrymore wyprawił do mnie\n",
              "grooma Perkinsa; przybyłem do Baskerville Hall w godzinę po\n",
              "katastrofie. Zbadałem wszystkie fakty, przytoczone w śledztwie.\n",
              "Szedłem śladami, pozostawionemi w Alei Wiązów, obejrzałem miejsce przy\n",
              "furtce, gdzie sir Karol zatrzymał się, i zmiarkowałem, że od owego\n",
              "miejsca szedł na palcach i że nie było śladu innych kroków, oprócz\n",
              "późniejszych, Barrymora. Wreszcie obejrzałem starannie zwłoki, które\n",
              "pozostały nietknięte do mego przybycia.\n",
              "\n",
              "„Sir Karol leżał nawznak z rozciągniętemi rękoma, jego twarz była\n",
              "tak zmieniona, że zaledwie mogłem ją poznać. Na ciele nie było\n",
              "żadnych obrażeń. Ale Barrymore w toku śledztwa uczynił jedno zeznanie\n",
              "fałszywe. Powiedział, że nie było żadnych śladów dokoła trupa. Nie\n",
              "spostrzegł ich, ale ja dostrzegłem -- zupełnie świeże, w pobliżu.\n",
              "\n",
              "-- Czy ślady kroków?\n",
              "\n",
              "-- Tak.\n",
              "\n",
              "-- Męzkie, czy kobiece?\n",
              "\n",
              "Doktor Mortimer patrzył na nas przez chwilę dziwnemi oczyma, wreszcie\n",
              "odparł szeptem:\n",
              "\n",
              "-- Panie Holmes, były ślady kroków... psa... olbrzymiego.\n",
              "\n",
              "\n",
              "\n",
              "\n",
              "III.\n",
              "\n",
              "Zagadka.\n",
              "\n",
              "\n",
              "Przyznaję, że gdym słuchał tego opowiadania, przebiegały po mnie\n",
              "dreszcze. I doktor był żywo poruszony.\n",
              "\n",
              "-- Widziałeś pan te ślady? -- spytałem.\n",
              "\n",
              "-- Tak, na własne oczy; tak wyraźnie, jak teraz widzę pana.\n",
              "\n",
              "-- I nic pan nie mówiłeś?\n",
              "\n",
              "-- Po co?\n",
              "\n",
              "-- Dlaczego nikt inny nie dostrzegł tych śladów?\n",
              "\n",
              "-- Pozostały o jakie dwa łokcie od trupa; nie zwrócono na nie uwagi\n",
              "i jabym ich nie zauważył, gdybym nie był znał tej legendy.\n",
              "\n",
              "-- Dużo jest psów w okolicy.\n",
              "\n",
              "-- Tak, ale to nie był zwykły pies. Powiedziałem już panom, że był\n",
              "ogromny.\n",
              "\n",
              "-- Czy nie zbliżył się do trupa?\n",
              "\n",
              "-- Nie.\n",
              "\n",
              "-- Czy noc była jasna?\n",
              "\n",
              "-- Nie; pochmurna i wilgotna.\n",
              "\n",
              "-- Ale deszcz nie padał?\n",
              "\n",
              "-- Nie.\n",
              "\n",
              "-- Jak wygląda aleja?\n",
              "\n",
              "-- Jest wysadzana dwoma rzędami wiązów i opasana żywopłotem,\n",
              "wysokości 12 stóp. Sama aleja ma 8 stóp szerokości.\n",
              "\n",
              "-- Czy jest co pomiędzy żywopłotem a aleją?\n",
              "\n",
              "-- Tak; po obu stronach biegnie trawnik, szerokości 6 stóp.\n",
              "\n",
              "-- O ile zrozumiałem, jest wejście przez furtkę?\n",
              "\n",
              "-- Tak; furtka prowadzi na łąkę.\n",
              "\n",
              "-- Czy są inne wejścia?\n",
              "\n",
              "-- Niema.\n",
              "\n",
              "-- Tak, iż aby wkroczyć w Aleję Wiązów, trzeba wyjść z domu, lub\n",
              "iść przez łąkę?\n",
              "\n",
              "-- Jest trzecie wejście przez altanę, na drugim końcu alei.\n",
              "\n",
              "-- Czy sir Karol doszedł do tej altany?\n",
              "\n",
              "-- Nie; znaleziono go o pięćdziesiąt łokci od niej.\n",
              "\n",
              "-- A teraz, zechciej mi pan powiedzieć, doktorze Mortimer -- będzie to\n",
              "szczegół ważny -- czy ślady, któreś pan spostrzegł, pozostały na\n",
              "żwirze, czy na trawie?\n",
              "\n",
              "-- Nie mogłem dojrzeć śladów na trawie.\n",
              "\n",
              "-- Czy ślady były po tej samej stronie, co furtka?\n",
              "\n",
              "-- Tak, po tej samej.\n",
              "\n",
              "-- Bardzo mnie pan zaciekawia. Jeszcze jedno pytanie. Czy furtka była\n",
              "zamknięta?\n",
              "\n",
              "-- Zamknięta na klucz i zaryglowana.\n",
              "\n",
              "-- Jak jest wysoka?\n",
              "\n",
              "-- Ma około 4-ch stóp.\n",
              "\n",
              "-- A więc można ją łatwo przesadzić?\n",
              "\n",
              "-- Tak.\n",
              "\n",
              "-- A jakie ślady znalazłeś pan przy furtce?\n",
              "\n",
              "-- Żadnych, któreby mogły zwrócić uwagę.\n",
              "\n",
              "-- Czyżeś pan nie oglądał ziemi dokoła?\n",
              "\n",
              "-- I owszem, oglądałem.\n",
              "\n",
              "-- I nie dostrzegłeś pan żadnych śladów?\n",
              "\n",
              "-- Były bardzo niewyraźne. Widocznie sir Karol stał tutaj przez pięć\n",
              "do dziesięciu minut.\n",
              "\n",
              "-- Skąd pan to wie?\n",
              "\n",
              "-- Gdyż popiół z cygara spadł dwa razy na ziemię.\n",
              "\n",
              "-- Wybornie. Znaleźliśmy kolegę wedle naszego serca. Prawda, Watson?\n",
              "Ale jakież to były ślady?\n",
              "\n",
              "-- Ślady stóp na żwirze. Innych znaków dostrzedz nie mogłem.\n",
              "\n",
              "Sherlock Holmes uderzył ręką w kolano.\n",
              "\n",
              "-- Czemu mnie tam nie było! -- zawołał. -- Jest to wypadek bardzo\n",
              "ciekawy, dający obfite pole do naukowej ekspertyzy. Czemuż nie mogłem\n",
              "odczytać tej żwirowej karty, zanim ją zatarły inne stopy! Doktorze\n",
              "Mortimer, że też mnie pan nie uwiadomił wcześniej! Jeśli nam się nie\n",
              "uda wyświetlić sprawy, cała odpowiedzialność spadnie na pana.\n",
              "\n",
              "-- Nie mogłem pana wzywać, panie Holmes, bo w takim razie musiałbym\n",
              "ujawnić te fakty przed całym światem, a mówiłem już, żem sobie tego\n",
              "nie życzył. Zresztą... zresztą...\n",
              "\n",
              "-- Czemu pan nie kończysz?\n",
              "\n",
              "-- Bywają dziedziny, niedostępne nawet dla najbystrzejszych\n",
              "i najdoświadczeńszych detektywów...\n",
              "\n",
              "-- Chcesz pan powiedzieć, że wchodzi to w zakres rzeczy\n",
              "nadprzyrodzonych?\n",
              "\n",
              "-- Tego nie twierdzę stanowczo.\n",
              "\n",
              "-- Ale pan tak myślisz w głębi ducha.\n",
              "\n",
              "-- Od owej tragedyi doszły do mojej świadomości fakty, sprzeczne\n",
              "z ogólnemi prawami natury.\n",
              "\n",
              "-- Naprzykład?\n",
              "\n",
              "-- Dowiaduję się, że przed tą katastrofą, kilku ludzi widziało na\n",
              "łące niezwykłe stworzenie, podobne do złego ducha Baskervillów.\n",
              "Wszyscy mówią, że był to pies ogromny, przezroczysty. Wypytywałem\n",
              "tych ludzi -- jeden z nich jest włościaninem, drugi kowalem,\n",
              "trzeci farmerem. Ich zeznania są jednakowe. W całej okolicy\n",
              "zapanował strach przesądny. Nikt po nocy nie przejdzie przez łąkę.\n",
              "\n",
              "-- I pan, człowiek nauki, wierzy w takie baśnie? -- zawołał Holmes.\n",
              "\n",
              "-- Dotychczas moje badania ogarniały świat zmysłów; usiłowałem\n",
              "walczyć z chorobą, ale ze złymi duchami walczyć nie umiem. Zresztą\n",
              "musisz pan przyznać, że ślady stóp są dowodem materyalnym. Pies\n",
              "Hugona nie był także zjawiskiem nadprzyrodzonem skoro mógł zagryźć\n",
              "na śmierć, a jednak miał w sobie coś szatańskiego.\n",
              "\n",
              "-- Widzę, że pan przeszedłeś do obozu spirytystów. Ale zechciej mi\n",
              "powiedzieć jeszcze jedno, doktorze Mortimer. Jeżeli pan skłaniasz się\n",
              "ku takim zapatrywaniom, dlaczego przyszedłeś zasięgnąć mojej rady?\n",
              "Powiadasz pan jednym tchem, że nie warto przeprowadzać śledztwa\n",
              "w sprawie zabójstwa sir Karola, a jednocześnie prosisz mnie, żebym się\n",
              "tą sprawą zajął.\n",
              "\n",
              "-- Nie prosiłem o to.\n",
              "\n",
              "-- Więc w jaki sposób mogę panu dopomódz?\n",
              "\n",
              "-- Radząc mi, co mam zrobić z sir Henrykiem Baskerville, który\n",
              "przybywa na dworzec Waterloo -- doktor Mortimer spojrzał na zegarek --\n",
              "za godzinę i kwadrans -- dokończył.\n",
              "\n",
              "-- Czy on jest spadkobiercą?\n",
              "\n",
              "-- Tak. Po śmierci sir Karola zasięgaliśmy wiadomości o tym\n",
              "gentlemanie i dowiedzieliśmy się, że ma fermę w Kanadzie. Wedle\n",
              "naszych informacyj, jest to młodzieniec bez zarzutu. Nie mówię teraz\n",
              "jako lekarz, lecz jako wykonawca testamentu sir Karola.\n",
              "\n",
              "-- Czy niema innych kandydatów do spadku?\n",
              "\n",
              "-- Żadnego. Mógłby nim być tylko Roger Baskerville, najmłodszy\n",
              "z trzech braci, z których sir Karol był najstarszym. Drugi brat, zmarły\n",
              "przedwcześnie, był właśnie ojcem owego Henryka. Trzeci, Roger, był\n",
              "synem marnotrawnym, żywą podobizną duchową starego Hugona. Tyle\n",
              "nabroił w Anglii, że nie mógł już tu przebywać, uciekł do Ameryki\n",
              "środkowej i umarł w roku 1876-ym na żółtą febrę. Henryk jest\n",
              "ostatnim z Baskervillów. Za godzinę i pięć minut mam go spotkać\n",
              "na dworcu Waterloo. Miałem depeszę z uwiadomieniem, iż przybył dziś\n",
              "do Southampton. A teraz, panie Holmes, powiedz, jak mi radzisz postąpić?\n",
              "\n",
              "-- Dlaczego sir Henryk nie miałby wrócić do domu swych ojców?\n",
              "\n",
              "-- Wydaje się to rzeczą naturalną, a jednak, jeśli weźmiemy pod\n",
              "uwagę, że każdego z Baskervillów, który tam przebywa, czeka śmierć\n",
              "nagła i gwałtowna... Jestem pewien, że gdyby sir Karol mógł był ze\n",
              "mną mówić przed katastrofą, byłby mi zakazał wprowadzać ostatniego\n",
              "z rodu do owej przeklętej rezydencyi. Z drugiej strony, dobrobyt\n",
              "całej okolicy zależy od przebywania dziedzica w Baskerville Hall.\n",
              "Całe dzieło, zapoczątkowane przez sir Karola, pójdzie w niwecz,\n",
              "jeśli nikt nie zamieszka na zamku. Boję się, aby dobro tej okolicy\n",
              "nie skłoniło mnie do nielojalnego postąpienia z sir Henrykiem\n",
              "i dlatego proszę pana o radę.\n",
              "\n",
              "Holmes zastanawiał się długą chwilę.\n",
              "\n",
              "-- Zatem -- odezwał się -- według pańskiego przekonania, jakaś siła\n",
              "nieczysta grozi w tych stronach Baskervillom. Wszak pan w to wierzy\n",
              "święcie?\n",
              "\n",
              "-- Gotów jestem przypuszczać, że tak jest.\n",
              "\n",
              "-- W takim razie, ta siła nieczysta może pastwić się nad Baskervillem\n",
              "równie dobrze w Londynie, jak i w Devonshire. Szatan, którego\n",
              "działanie byłoby umiejscowione, nie byłby groźnym.\n",
              "\n",
              "-- Starasz się pan ośmieszyć moją obawę, panie Holmes. Lecz gdybyś\n",
              "sam widział te rzeczy nadprzyrodzone, odechciałoby ci się żartów.\n",
              "Z pańskich słów miarkuję, że ów młodzieniec jest równie bezpieczny\n",
              "w Londynie, jak i w Devonshire. Przybywa za pięćdziesiąt minut. Cóż\n",
              "mi pan radzisz?\n",
              "\n",
              "-- Radzę wziąć dorożkę, zawołać psa, który skowyczy za drzwiami\n",
              "i jechać na dworzec Waterloo na spotkanie sir Henryka Baskerville.\n",
              "\n",
              "-- A potem?\n",
              "\n",
              "-- A potem nic mu pan nie powiesz, dopóki nie namyślę się w tym\n",
              "względzie.\n",
              "\n",
              "-- Jak długo potrzebujesz pan namyślać się?\n",
              "\n",
              "-- Dwadzieścia cztery godziny. Poproszę cię, doktorze Mortimer, abyś\n",
              "mnie odwiedził jutro rano o dziesiątej. A zechciej przywieźć ze sobą\n",
              "sir Henryka Baskerville. To mi ułatwi wykonanie mego planu.\n",
              "\n",
              "-- Zrobię, jak pan chcesz.\n",
              "\n",
              "Doktor zapisał godzinę na mankiecie i wyszedł. Pan Holmes zatrzymał go\n",
              "na schodach.\n",
              "\n",
              "-- Jeszcze jedno pytanie -- rzekł. -- Powiadasz pan, że przed śmiercią\n",
              "sir Karola kilku ludzi widziało to zjawisko na łące?\n",
              "\n",
              "-- Tak, trzech ludzi.\n",
              "\n",
              "-- Czy który z nich widział je potem?\n",
              "\n",
              "-- Nie wiem.\n",
              "\n",
              "-- Dziękuję panu. Dowidzenia.\n",
              "\n",
              "Holmes powrócił na swoje miejsce. Był widocznie zadowolony.\n",
              "\n",
              "-- Wychodzisz, Watson? -- rzekł.\n",
              "\n",
              "-- Czy potrzebujesz mojej pomocy?\n",
              "\n",
              "-- Nie, mój drogi. Poproszę cię o nią dopiero w chwili działania.\n",
              "Sprawa wyjątkowa. Przechodząc obok Bradleya, zechciej wstąpić do\n",
              "sklepu i każ mi przynieść paczkę najmocniejszego tytoniu. Jeżeli\n",
              "możesz, byłbym ci bardzo obowiązany, gdybyś tu nie wracał przed\n",
              "wieczorem, a wtedy zestawimy nasze wrażenia i poglądy.\n",
              "\n",
              "Wiedziałem, że samotność jest niezbędną mojemu przyjacielowi\n",
              "w chwilach, gdy zastanawiał się nad poszlakami spraw kryminalnych, gdy\n",
              "wyciągał wnioski i tworzył teorye, które okazywały się zawsze\n",
              "słusznemi. To też cały dzień spędziłem w klubie i dopiero około\n",
              "dziewiątej powróciłem do mieszkania przy Bakerstreet.\n",
              "\n",
              "Gdy drzwi otworzyłem, zdało mi się, że w mieszkaniu był pożar;\n",
              "światło lampy ukazywało się jakby za czarną mgłą. Po chwili\n",
              "zmiarkowałem, że dym pochodzi nie od ognia, lecz od mocnego tytoniu.\n",
              "Wśród kłębów ujrzałem Holmesa w szlafroku. Siedział w fotelu\n",
              "z fajeczką w ustach. Na stole leżało kilka zwitków papieru.\n",
              "Odkaszlnąłem.\n",
              "\n",
              "-- Zaziębiłeś się? -- spytał.\n",
              "\n",
              "-- Nie, ale można się tu udusić.\n",
              "\n",
              "-- Otwórz okno. Widzę, że spędziłeś cały dzień w klubie...\n",
              "\n",
              "-- Po czem to miarkujesz, Holmes?\n",
              "\n",
              "-- Jesteś rzeźwy, pachnący, w dobrym humorze. Nigdy nie domyślisz się,\n",
              "gdzie ja byłem.\n",
              "\n",
              "-- Nie będę nad tem suszył głowy. Powiesz mi sam.\n",
              "\n",
              "-- A więc byłem w Devonshire.\n",
              "\n",
              "-- Myślą?\n",
              "\n",
              "-- Tak. Moje ciało pozostało tutaj, na tym fotelu, i skonsumowało dwa\n",
              "olbrzymie imbryki kawy i niezliczoną moc tytoniu. Po twojem wyjściu\n",
              "posłałem do Stamforda po mapę tej okolicy i błądziłem po niej przez\n",
              "dzień cały. Pochlebiam sobie, że każda piędź ziemi jest mi teraz\n",
              "dobrze znana.\n",
              "\n",
              "-- To zapewne mapa o wielkiej skali?\n",
              "\n",
              "-- Tak.\n",
              "\n",
              "Rozwinął ją i położył na kolanie.\n",
              "\n",
              "-- Widzisz -- mówił -- oto łąka, a to -- Baskerville Hall.\n",
              "\n",
              "-- Naokoło las.\n",
              "\n",
              "-- Istotnie. A oto Aleja Wiązów, na lewo od łąki. Tu jest wioska\n",
              "Grimpen, w której doktor Mortimer obrał swoją główną kwaterę.\n",
              "W obrębie mil pięciu mało jest ludzkich siedzib. Oto Lafter Hall.\n",
              "Tu -- domek przyrodnika Stapleton. Tutaj dwie formy: High Tore\n",
              "i Fulmire. O czternaście mil dalej -- więzienie państwowe Princetown.\n",
              "Dokoła i pośrodku -- łąki i trzęsawiska. Taki jest teren, na którym\n",
              "rozegrała się owa tragedya. Postaramy się odtworzyć wszystkie jej\n",
              "sceny i akty.\n",
              "\n",
              "-- Miejscowość bezludna.\n",
              "\n",
              "-- Tak. Dyabeł mógł się na niej rozgościć...\n",
              "\n",
              "-- A zatem i pan skłaniasz się do nadprzyrodzonych wyjaśnień..."
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "Markdown(chunks[1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "MjC04LezATAT"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "## The Hound of the Baskervilles (Translated from Polish)\n",
              "\n",
              "Dr. Mortimer finished reading this extraordinary tale, pushed his spectacles up on his forehead, and looked at Mr. Sherlock Holmes. The latter yawned and tossed his cigarette into the fire.\n",
              "\n",
              "\"Well?\" he said.\n",
              "\n",
              "\"Is not this history interesting?\"\n",
              "\n",
              "\"Assuredly, for a collector of old legends and fairy tales.\"\n",
              "\n",
              "Dr. Mortimer pulled a newspaper from his pocket.\n",
              "\n",
              "\"And now, Mr. Holmes,\" he said, \"I shall show you something more current. Here is the 'Devon County Chronicle' from the 14th of May of the current year. It contains an account of the facts pertaining to the death of Sir Charles Baskerville, which occurred a few days before that date.\"\n",
              "\n",
              "My friend leaned forward and began to listen more attentively. Dr. Mortimer read:\n",
              "\n",
              "\"The recent and sudden death of Sir Charles Baskerville, the Liberal candidate for the county of Devon, has filled our entire district with astonishment and trepidation. Although Sir Charles Baskerville had only resided at Baskerville Hall for a short time, his courtesy, generosity, and nobility had earned him the respect of all who came into contact with him. In this age of parvenus, it is pleasant to see a descendant of an ancient and decaying family regaining the fortune of his ancestors and restoring luster to an old name.\n",
              "\n",
              "\"Sir Charles, as is known, made a large fortune in speculations in South Africa, and being naturally cautious, he no longer tempted fate, which might turn away from him, as it does from others, and having realized his fortune, he returned to England.\n",
              "\n",
              "\"He had only settled in Baskerville Hall two years ago; his extensive plans for renovating the old residence and introducing improvements in agriculture are well known. Death has prevented their realization.\n",
              "\n",
              "\"Being childless, he desired that the entire district should benefit from his fortune, and many of his neighbors have significant reasons to mourn his untimely demise. We have often reported in these columns his generous donations to public institutions.\n",
              "\n",
              "\"The inquest has not yet clarified the circumstances surrounding the death of Sir Charles. The most bizarre legends are circulating here, attributing this sudden death to the action of supernatural forces.\n",
              "\n",
              "\"Sir Charles was a widower, and generally considered an eccentric. Despite his large fortune, he had simple, modest habits. The staff at Baskerville Hall consisted of a butler named Barrymore, his wife, a cook and housekeeper in one. Both testify that Sir Charles' health had been failing recently, that a heart condition was developing, manifesting itself in paleness, shortness of breath, and frequent fainting spells. Dr. George Mortimer, a friend and family physician of the deceased, testified to the same effect.\n",
              "\n",
              "\"Sir Charles Baskerville was accustomed to taking a walk every evening, before retiring to bed, along the famous Yew Alley in front of the manor. Mr. and Mrs. Barrymore relate that on the evening of the 4th of May, their master announced that he would be departing for London the next day and ordered them to pack his trunk.\n",
              "\n",
              "\"That evening, he went for a walk as usual, during which he smoked a cigar.\n",
              "\n",
              "\"He never returned from this walk.\n",
              "\n",
              "\"At midnight, Barrymore, seeing the front door still open, became concerned and, taking a lantern, went to look for his master. It had rained during the day, so Sir Charles' footprints were easily visible along the alley. Halfway down, there is a gate leading to the moor.\n",
              "\n",
              "\"There were signs that Sir Charles had stood by it for some time, then turned back into the alley. His body was found at the end of it.\n",
              "\n",
              "\"Among other things, one fact arising from Barrymore's testimony remains unexplained, namely that the footprints of his master changed from the moment he stood by the gate and that from then on he walked on tiptoe.\n",
              "\n",
              "\"A certain Murphy, a Gypsy horse trader, was on the moor at the time, at a distance from the deceased, but he admits that he was drunk. He says he heard screams, but cannot determine where they came from.\n",
              "\n",
              "\"There were no signs of violence or assault on Sir Charles' body, and although the doctor testifies that the expression on the deceased's face was so altered that he, Dr. Mortimer, could not at first recognize him, experts attribute this to a heart aneurysm, which can cause similar symptoms.\n",
              "\n",
              "\"The post-mortem examination revealed a pre-existing heart defect; the coroner confirmed the medical examination. It is desirable that Sir Charles' heir should take possession of the estate as soon as possible and continue the glorious work that was so suddenly and tragically interrupted.\n",
              "\n",
              "\"If the prosaic verdict of the coroner and the experts did not put an end to the romantic stories circulating about this death, it would not be easy to find an owner for Baskerville Hall.\n",
              "\n",
              "\"The presumed heir is Mr. Henry Baskerville, son of Sir Charles' younger brother. This young man departed for America a few years ago. A search has been ordered. He will likely soon learn of the change in his fortunes.\"\n",
              "\n",
              "*       *       *       *       *\n",
              "\n",
              "Dr. Mortimer folded the newspaper and slipped it into his pocket, saying:\n",
              "\n",
              "\"These are the facts, commonly known.\"\n",
              "\n",
              "\"Thank you for drawing my attention to a truly interesting case,\" said Mr. Holmes. \"I read about it in the newspapers, admittedly, but at that time I was preoccupied with the cameos that had disappeared from the Vatican Museum. I wanted to be of service to the Pope and lost sight of what was happening in England simultaneously. This article, you say, contains the facts, commonly known?\"\n",
              "\n",
              "\"Yes.\"\n",
              "\n",
              "\"Then please inform me now of what no one knows.\"\n",
              "\n",
              "Sherlock Holmes leaned back in his armchair and again joined his hands so that his fingertips touched.\n",
              "\n",
              "\"I shall,\" said Dr. Mortimer, betraying increasing agitation. \"I will tell you what I have not told anyone yet. I concealed it from the coroner for reasons that you will surely understand. As a man of science, I did not want to publicly admit that I believe in superstitions. Furthermore, I understood that if the belief in supernatural forces governing Baskerville Hall became established, no one would be willing to take possession of the old manor. For these two reasons, I did not testify before the court to everything I know – for it would have been of no use to the coroner – but with you, I will be completely frank.\n",
              "\n",
              "\"The district is sparsely populated, the residences are rare, and the peasant dwellings are scattered at considerable distances from each other.\n",
              "\n",
              "\"I often saw Sir Charles Baskerville, who was thirsty for company. Apart from Mr. Frankland in Lafterhall and the naturalist, Mr. Stapleton, there are no intelligent people nearby. Sir Charles was secretive, taciturn. We became close because of his illness, and also because we shared scientific interests. He brought many interesting accounts from South Africa. We spent many a pleasant evening discussing comparative anatomy.\n",
              "\n",
              "\"Over the last few months, I noticed that Sir Charles' nervous system was strained. He took the legend so much to heart that although he took walks every evening, nothing could induce him to enter the moor after sunset. He was constantly haunted by the fear of ghosts, and often asked me if I had seen some strange creature or heard barking. He always asked these questions in a trembling voice.\n",
              "\n",
              "\"One of my visits to him three weeks ago is impressed on my memory. He was standing in the hall. As I got out of the carriage, I noticed that he was looking over my shoulder with fear in his eyes. I turned around and saw some strange shape, similar to a large black calf. It passed behind my carriage.\n",
              "\n",
              "\"Sir Charles was so agitated that I went to look for this peculiar animal. But it was nowhere to be found. My patient could not calm down. I stayed with him all evening, and then, explaining his agitation, he told me the whole story.\n",
              "\n",
              "\"I mention this minor episode because it takes on significance in light of the tragedy that followed; at the time, I did not attach much importance to it and was surprised by Sir Charles' agitation.\n",
              "\n",
              "\"He was to go to London on my advice. I knew that he had a heart condition and that constant anxiety was bad for his health. I thought that a few months spent in leisure would free him from these nightmares and visions. Our mutual friend, Mr. Stapleton, was of the same opinion. And here is the result: misfortune.\n",
              "\n",
              "\"Immediately after Sir Charles' death, the butler Barrymore sent the groom Perkins to me; I arrived at Baskerville Hall within an hour of the catastrophe. I examined all the facts cited in the inquest. I followed the tracks left in the Yew Alley, examined the place by the gate where Sir Charles had stopped, and realized that from that place he had walked on tiptoe and that there was no trace of other footsteps, apart from Barrymore's later ones. Finally, I examined the body carefully, which remained untouched until my arrival.\n",
              "\n",
              "\"Sir Charles lay on his back with his arms outstretched, his face so altered that I could hardly recognize it. There were no injuries on the body. But Barrymore made one false statement in the course of the investigation. He said that there were no tracks around the body. He did not notice them, but I did – quite fresh, nearby.\"\n",
              "\n",
              "\"Footprints?\"\n",
              "\n",
              "\"Yes.\"\n",
              "\n",
              "\"Male or female?\"\n",
              "\n",
              "Dr. Mortimer looked at us for a moment with strange eyes, finally replying in a whisper:\n",
              "\n",
              "\"Mr. Holmes, there were the footprints... of a dog... a gigantic one.\"\n",
              "\n",
              "III.\n",
              "\n",
              "The Riddle.\n",
              "\n",
              "I confess that as I listened to this tale, shivers ran down my spine. And the doctor was visibly moved.\n",
              "\n",
              "\"You saw these footprints?\" I asked.\n",
              "\n",
              "\"Yes, with my own eyes; as clearly as I see you now.\"\n",
              "\n",
              "\"And you said nothing?\"\n",
              "\n",
              "\"What for?\"\n",
              "\n",
              "\"Why didn't anyone else notice these tracks?\"\n",
              "\n",
              "\"They were about two yards from the body; no attention was paid to them, and I would not have noticed them if I had not known the legend.\"\n",
              "\n",
              "\"There are many dogs in the area.\"\n",
              "\n",
              "\"Yes, but this was no ordinary dog. I have already told you that it was enormous.\"\n",
              "\n",
              "\"Did it not approach the body?\"\n",
              "\n",
              "\"No.\"\n",
              "\n",
              "\"Was the night clear?\"\n",
              "\n",
              "\"No; cloudy and damp.\"\n",
              "\n",
              "\"But it was not raining?\"\n",
              "\n",
              "\"No.\"\n",
              "\n",
              "\"What does the alley look like?\"\n",
              "\n",
              "\"It is lined with two rows of yew trees and surrounded by a hedge, 12 feet high. The alley itself is 8 feet wide.\"\n",
              "\n",
              "\"Is there anything between the hedge and the alley?\"\n",
              "\n",
              "\"Yes; on both sides runs a lawn, 6 feet wide.\"\n",
              "\n",
              "\"As I understand it, there is an entrance through the gate?\"\n",
              "\n",
              "\"Yes; the gate leads to the moor.\"\n",
              "\n",
              "\"Are there other entrances?\"\n",
              "\n",
              "\"No.\"\n",
              "\n",
              "\"So, in order to enter the Yew Alley, one must leave the house or go through the moor?\"\n",
              "\n",
              "\"There is a third entrance through the gazebo, at the other end of the alley.\"\n",
              "\n",
              "\"Did Sir Charles reach this gazebo?\"\n",
              "\n",
              "\"No; he was found fifty yards from it.\"\n",
              "\n",
              "\"And now, please tell me, Dr. Mortimer – this will be an important detail – were the tracks you spotted on the gravel or on the grass?\"\n",
              "\n",
              "\"I could not see any tracks on the grass.\"\n",
              "\n",
              "\"Were the tracks on the same side as the gate?\"\n",
              "\n",
              "\"Yes, on the same side.\"\n",
              "\n",
              "\"You intrigue me greatly. One more question. Was the gate locked?\"\n",
              "\n",
              "\"Locked and bolted.\"\n",
              "\n",
              "\"How high is it?\"\n",
              "\n",
              "\"About 4 feet.\"\n",
              "\n",
              "\"So it can be easily climbed over?\"\n",
              "\n",
              "\"Yes.\"\n",
              "\n",
              "\"And what tracks did you find near the gate?\"\n",
              "\n",
              "\"None that could attract attention.\"\n",
              "\n",
              "\"Did you not examine the ground around?\"\n",
              "\n",
              "\"Indeed, I did.\"\n",
              "\n",
              "\"And you didn't notice any tracks?\"\n",
              "\n",
              "\"They were very indistinct. Apparently Sir Charles stood here for five to ten minutes.\"\n",
              "\n",
              "\"How do you know that?\"\n",
              "\n",
              "\"Because the ash from his cigar fell to the ground twice.\"\n",
              "\n",
              "\"Excellent. We have found a colleague after our own heart. True, Watson? But what kind of tracks were they?\"\n",
              "\n",
              "\"Footprints on the gravel. I could discern no other signs.\"\n",
              "\n",
              "Sherlock Holmes struck his knee with his hand.\n",
              "\n",
              "\"Why was I not there!\" he exclaimed. \"This is a very interesting case, providing ample scope for scientific expertise. Why could I not read this gravelly card before other feet had obscured it! Dr. Mortimer, that you did not inform me sooner! If we fail to shed light on the matter, all the responsibility will fall on you.\"\n",
              "\n",
              "\"I could not call you, Mr. Holmes, because in that case I would have had to reveal these facts to the whole world, and I have already said that I did not wish to do so. Besides... besides...\"\n",
              "\n",
              "\"Why don't you finish?\"\n",
              "\n",
              "\"There are areas inaccessible even to the most astute and experienced detectives...\"\n",
              "\n",
              "\"Do you mean to say that this falls within the realm of the supernatural?\"\n",
              "\n",
              "\"I do not assert that definitively.\"\n",
              "\n",
              "\"But you think so in your heart of hearts.\"\n",
              "\n",
              "\"Since that tragedy, facts have come to my attention that contradict the general laws of nature.\"\n",
              "\n",
              "\"For example?\"\n",
              "\n",
              "\"I am informed that before the catastrophe, several people saw on the moor an unusual creature, similar to the evil spirit of the Baskervilles. Everyone says that it was a huge, translucent dog. I questioned these people – one of them is a peasant, the second a blacksmith, the third a farmer. Their testimonies are consistent. A superstitious fear has gripped the entire district. No one will cross the moor after dark.\"\n",
              "\n",
              "\"And you, a man of science, believe in such tales?\" exclaimed Holmes.\n",
              "\n",
              "\"Until now, my research has encompassed the world of the senses; I have striven to combat disease, but I do not know how to fight evil spirits. Besides, you must admit that the footprints are material evidence. Hugo's dog was also not a supernatural phenomenon since it could bite to death, yet it had something diabolical about it.\"\n",
              "\n",
              "\"I see that you have gone over to the camp of the spiritualists. But please tell me one more thing, Dr. Mortimer. If you incline towards such views, why did you come to seek my advice? You say in one breath that it is not worth conducting an investigation into the murder of Sir Charles, and at the same time, you ask me to take up the case.\"\n",
              "\n",
              "\"I did not ask you to do that.\"\n",
              "\n",
              "\"Then how can I help you?\"\n",
              "\n",
              "\"By advising me what to do with Sir Henry Baskerville, who arrives at Waterloo Station,\" Dr. Mortimer glanced at his watch, \"in an hour and a quarter,\" he finished.\n",
              "\n",
              "\"Is he the heir?\"\n",
              "\n",
              "\"Yes. After Sir Charles' death, we sought information about this gentleman and learned that he has a farm in Canada. According to our information, he is a young man without reproach. I speak now not as a doctor, but as an executor of Sir Charles' will.\"\n",
              "\n",
              "\"Are there no other candidates for the inheritance?\"\n",
              "\n",
              "\"None. It could only be Roger Baskerville, the youngest of the three brothers, of whom Sir Charles was the eldest. The second brother, who died prematurely, was the father of this Henry. The third, Roger, was a prodigal son, a living spiritual likeness of old Hugo. He made so much trouble in England that he could no longer stay here, fled to Central America, and died in 1876 of yellow fever. Henry is the last of the Baskervilles. In an hour and five minutes, I am to meet him at Waterloo Station. I had a telegram informing me that he arrived in Southampton today. And now, Mr. Holmes, tell me, what do you advise me to do?\"\n",
              "\n",
              "\"Why should Sir Henry not return to the home of his fathers?\"\n",
              "\n",
              "\"It seems a natural thing to do, and yet, if we consider that every Baskerville who resides there awaits sudden and violent death... I am certain that if Sir Charles could have spoken to me before the catastrophe, he would have forbidden me to bring the last of the line into that accursed residence. On the other hand, the prosperity of the entire district depends on the heir residing at Baskerville Hall. All the work begun by Sir Charles will be in vain if no one lives in the manor. I fear that the good of this district will tempt me to act disloyally with Sir Henry, and therefore I ask you for advice.\"\n",
              "\n",
              "Holmes pondered for a long while.\n",
              "\n",
              "\"So,\" he said, \"according to your conviction, some unclean force threatens the Baskervilles in those parts. You believe that firmly, do you not?\"\n",
              "\n",
              "\"I am ready to assume that is the case.\"\n",
              "\n",
              "\"In that case, this unclean force can prey on Baskerville as well in London as in Devonshire. A Satan whose influence would be localized would not be threatening.\"\n",
              "\n",
              "\"You are trying to ridicule my fear, Mr. Holmes. But if you yourself had seen these supernatural things, you would lose your taste for jokes. From your words, I gather that this young man is as safe in London as he is in Devonshire. He arrives in fifty minutes. What do you advise me?\"\n",
              "\n",
              "\"I advise you to take a cab, call the dog that is whining behind the door, and go to Waterloo Station to meet Sir Henry Baskerville.\"\n",
              "\n",
              "\"And then?\"\n",
              "\n",
              "\"And then you will tell him nothing until I have made up my mind in this regard.\"\n",
              "\n",
              "\"How long do you need to make up your mind?\"\n",
              "\n",
              "\"Twenty-four hours. I shall ask you, Dr. Mortimer, to visit me tomorrow morning at ten o'clock. And please bring Sir Henry Baskerville with you. That will make it easier for me to execute my plan.\"\n",
              "\n",
              "\"I will do as you wish.\"\n",
              "\n",
              "The doctor wrote down the time on his cuff and left. Mr. Holmes stopped him on the stairs.\n",
              "\n",
              "\"One more question,\" he said. \"You say that before the death of Sir Charles, several people saw this apparition on the moor?\"\n",
              "\n",
              "\"Yes, three people.\"\n",
              "\n",
              "\"Did any of them see it afterwards?\"\n",
              "\n",
              "\"I do not know.\"\n",
              "\n",
              "\"Thank you. Goodbye.\"\n",
              "\n",
              "Holmes returned to his place. He was visibly pleased.\n",
              "\n",
              "\"You are going out, Watson?\" he said.\n",
              "\n",
              "\"Do you need my help?\"\n",
              "\n",
              "\"No, my dear fellow. I will ask for it only at the moment of action. An exceptional case. As you pass Bradley's, please stop in the shop and have them bring me a package of the strongest tobacco. If you can, I would be very obliged if you did not return here before evening, and then we will compare our impressions and views.\"\n",
              "\n",
              "I knew that solitude was essential to my friend in moments when he was pondering the clues of criminal cases, when he was drawing conclusions and forming theories that always proved to be correct. So I spent the whole day at the club and only returned to the apartment on Baker Street around nine o'clock.\n",
              "\n",
              "When I opened the door, it seemed to me that there was a fire in the apartment; the light of the lamp appeared as if through a black fog. After a moment, I realized that the smoke came not from fire, but from strong tobacco. Amidst the fumes, I saw Holmes in his dressing gown. He was sitting in an armchair with a pipe in his mouth. Several rolls of paper lay on the table. I coughed.\n",
              "\n",
              "\"Have you caught a cold?\" he asked.\n",
              "\n",
              "\"No, but one can suffocate in here.\"\n",
              "\n",
              "\"Open the window. I see you spent the whole day at the club...\"\n",
              "\n",
              "\"How do you know that, Holmes?\"\n",
              "\n",
              "\"You are sober, fragrant, in good humor. You will never guess where I have been.\"\n",
              "\n",
              "\"I will not rack my brain over it. You will tell me yourself.\"\n",
              "\n",
              "\"Well then, I have been to Devonshire.\"\n",
              "\n",
              "\"In thought?\"\n",
              "\n",
              "\"Yes. My body remained here, in this armchair, and consumed two huge teapots of coffee and an uncountable amount of tobacco. After you left, I sent to Stamford for a map of that area and wandered over it all day. I flatter myself that every inch of ground is now well known to me.\"\n",
              "\n",
              "\"It is probably a large-scale map?\"\n",
              "\n",
              "\"Yes.\"\n",
              "\n",
              "He unfolded it and laid it on his knee.\n",
              "\n",
              "\"You see,\" he said, \"here is the moor, and here – Baskerville Hall.\"\n",
              "\n",
              "\"Around it, the forest.\"\n",
              "\n",
              "\"Indeed. And here is the Yew Alley, to the left of the moor. Here is the village of Grimpen, where Dr. Mortimer has established his headquarters. Within a five-mile radius, there are few human dwellings. Here is Lafter Hall. Here – the cottage of the naturalist Stapleton. Here are two farms: High Tore and Fulmire. Fourteen miles further – the Princetown state prison. Around and in the middle – moors and swamps. Such is the terrain on which that tragedy unfolded. We will try to recreate all its scenes and acts.\"\n",
              "\n",
              "\"A desolate place.\"\n",
              "\n",
              "\"Yes. The devil could make himself at home there...\"\n",
              "\n",
              "\"So, you too are inclined towards supernatural explanations...\"\n"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "Markdown(results[1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "id": "jXlAr8smUzV_"
      },
      "outputs": [],
      "source": [
        "translated_texts = [result[0] for result in results if isinstance(result, tuple)]\n",
        "\n",
        "# join the results together by double whitespace\n",
        "translated_book = \"\\n\\n\".join(translated_texts)\n",
        "\n",
        "# save the translated book\n",
        "with open(f\"/content/translated_book.txt\", \"w\") as f:\n",
        "  f.write(translated_book)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eA-ro5jwk_uw"
      },
      "source": [
        "## Next steps\n",
        "\n",
        "Now you know how to properly prepare data, check token limitations and translate each chunk to obtain your results. Feel free to try this process with a book that interests you!"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "Translate_a_Public_Domain_Book.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
